<?php

namespace backend\controllers;

use Yii;
use backend\models\PayTradeRebate;
use backend\models\PayTradeRebateSearch;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
use backend\models\PayRebateUsertake;
use frontend\models\SendSms;
use common\models\UserInfo;
use MakemoneyApi;
use backend\models\MakemoneyLog;
include_once "../../vendor/alipay/pagepay/makemoneyApi.php";
/**
 * IntegralController implements the CRUD actions for PayTradeRebate model.
 */
class PaymentController extends Controller
{
    public function behaviors()
    {
        return [
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                    'delete' => ['post'],
                ],
            ],
        ];
    }
   
    
    
    public function actionIndex(){
        echo "test";
    }
    
    /**
     * 提现
     */
    public function actionCheckMakemoney(){
        $post=Yii::$app->request->post();
         $idlist=$post['ids'];
        $return_result=array("code"=>'1',"msg"=>'打款成功',"pid"=>0);
        if($aliAccount!=""){
            $result=$this->actionAlipay($aliAccount, $take_money);
            $result=json_decode($result);
            $result=(array)$result;
            $code=$result['code'];
            $msg=$result['msg'];
            if($code=="10000"){//转账成功
            }else{
            }
        }else{
            $result=$this->actionSettle($wxOpenid, $take_money);
            $result=json_decode($result);
            $result=(array)$result;
            $code=$result['code'];
            $msg=$result['msg'];
            if($code=="1"){//转账成功
            }else{
            }
        }//微信打款 结束标签
        echo json_encode($return_result,true);exit();
    }
    protected function actionAlipay( $payee_account, $account){
        if($payee_account==""){
            $response_result=array("code"=>2222,"msg"=>"支付宝账号为空！","order_id"=>"无","pay_date"=>"无","sign"=>"无");
            return json_encode($response_result); 
        }elseif(doubleval($account)<0.1){
            $response_result=array("code"=>3333,"msg"=>"单笔金额不可小于0.1元！","order_id"=>"无","pay_date"=>"无","sign"=>"无");
            return json_encode($response_result);
        }   
        $tm = date('Ymdhis');
        $at = intval(rand(0,999999));
        $config['appId']=Yii::$app->params['alipay']['appid'];
        $config['rsaPrivateKey']=Yii::$app->params['alipay']['rsaPrivateKey'];
        $config['alipayrsaPublicKey']=Yii::$app->params['alipay']['alipayrsaPublicKey'];
        $param['out_biz_no']= $tm.$at;
        $param['payee_account']=$payee_account;//收款方账户
        $param['account']=$account;//转账金额 必须大于等于0.1元
        $param['payer_show_name']=Yii::$app->params['siteCompany'];//付款方姓名
        $param['remark']="提现";//转账备注
        $makemoneyApi=new  makemoneyApi();
        $responseObj= makemoneyApi::makemoney($config, $param);
        $responseArray=json_decode($responseObj);
        $responseArray=(array)$responseArray;
        return  json_encode($responseArray);
    }
    /**
     * 微信企业接口转账
     * @param   $openid
     * @param  $amount
     * @return string
     */
    public function actionSettle($openid,$amount) {//
       /*  $openid="of7IXt9zB3HfNmP7IDTc9Akw53ss";
        $amount=0.1; */
         if (doubleval($amount) >20000) {//单笔2万交易额 
            return json_encode(array("code"=>4,"msg"=>"单笔提现2万元，以防有诈！终止提现"));
        }elseif(doubleval($amount)<1){
            return json_encode(array("code"=>3,"msg"=>"提现不能少于1元"));
        }
        $amount = doubleval($amount)* 100;//单位  分
        if (doubleval($amount) <100) {
            return json_encode(array("code"=>3,"msg"=>"提现不能少于1元")); 
        }  
        $package['mch_appid'] = Yii::$app->params['weixin']['WX_APPID'];
        $package['mchid'] =  Yii::$app->params['weixin']['WX_MCHID'];
        $package['nonce_str'] =  PayRebateUsertake::getRandChar(15);
        $package['check_name'] = 'NO_CHECK';
        $package['partner_trade_no'] = Yii::$app->params['weixin']['WX_MCHID'].date("YmdHis");
        $package['openid'] = $openid;
        $package['re_user_name'] = "";//收款用户姓名 可选
        $package['amount'] = $amount;
        $package['desc'] = '过夹夹设计师提现';
        $package['spbill_create_ip'] =Yii::$app->request->userIP;
        $package =PayRebateUsertake::secretkeyAction($package);
        $dat = PayRebateUsertake::array2xml($package);
        $url = 'https://api.mch.weixin.qq.com/mmpaymkttransfers/promotion/transfers';
        //退款 https://api.mch.weixin.qq.com/secapi/pay/refund
        $responseXml = PayRebateUsertake::curl_post_ssl($url, $dat);
        $responseObj = simplexml_load_string($responseXml, 'SimpleXMLElement', LIBXML_NOCDATA);
        $responseArr = (array)$responseObj;
        //企业支付出错
        if ($responseArr['result_code'] == 'FAIL') {
            $payresult= json_encode(array("code"=>2,"msg"=>$responseArr['return_msg'],
                "partner_trade_no"=>"无",
                "payment_no"=>"无",
                "payment_time"=>"无"
            ));
        }elseif($responseArr['result_code'] == 'SUCCESS'){
            $payresult=json_encode(array("code"=>1,"msg"=>"支付成功",
                "partner_trade_no"=>$responseArr['partner_trade_no'],
                "payment_no"=>$responseArr['payment_no'],
                "payment_time"=>$responseArr['payment_time']
            )); 
        }
         return $payresult;
    }
   
    
    public function actionRefund(){
        $transaction_id="4200000133201807137509550336";
        $amount=0.01;
        $result=$this->actionWechatRefund($transaction_id, $amount);
        $result=json_decode($result);
        $result=(array)$result;
        $code=$result['code'];
        $msg=$result['msg'];
        echo "返回结果:<br/>";
        print_R($result);
        echo "<br/>code:".$code."  msg:".$msg;
    }
    
    /**
     * 退款
     * @param   out_refund_no  商户系统内部的退款单号，商户系统内部唯一，只能是数字、大小写字母_-|*@ ，同一退款单号多次请求只退一笔。
     * @param   $transaction_id  微信生成的订单号
     * @return string
     */
    
    public function actionWechatRefund($transaction_id,$amount) {//
        $amount = doubleval($amount)* 100;//单位  分
        $package['appid'] = Yii::$app->params['weixin']['WX_APPID'];
        $package['mch_id'] =  Yii::$app->params['weixin']['WX_MCHID'];
        $package['nonce_str'] =  PayRebateUsertake::getRandChar(15);
        $package['transaction_id']=$transaction_id;
        $package['out_refund_no']=Yii::$app->params['weixin']['WX_MCHID'].date("YmdHis").rand(0,100);
        $package['total_fee'] = $amount;
        $package['refund_fee']=$amount;
        $package =PayRebateUsertake::secretkeyAction($package);
        $dat = PayRebateUsertake::array2xml($package);
        $url = 'https://api.mch.weixin.qq.com/secapi/pay/refund';
        $responseXml = PayRebateUsertake::curl_post_ssl($url, $dat);
        $responseObj = simplexml_load_string($responseXml, 'SimpleXMLElement', LIBXML_NOCDATA);
        $responseArr = (array)$responseObj;
        //企业支付出错
        if ($responseArr['return_code'] == 'FAIL') {
            $payresult= json_encode(array("code"=>2,"msg"=>$responseArr['return_msg'],
                "partner_trade_no"=>"无",
                "payment_no"=>"无",
                "payment_time"=>"无"
            ));
        }elseif($responseArr['result_code'] == 'SUCCESS'){
            $payresult=json_encode(array("code"=>1,"msg"=>"支付成功",
            ));
        }
        return $payresult;
    }
}
